﻿/*
* Актор - звено с передаточной функцией k*(T1*s+1)/(T2*s+1)
* */

using System;
using System.Collections.Generic;
using System.Text;

using ctem.kernel;
using ctem.kernel.lib;
using ctem.kernel.lib.ports.sequential;


namespace ctem.samples.actors.sequential
{
    public class IntegrDiff : Actor
    {
        OutPort outport;
        InPort inport;

        DoubleParam step;
        DoubleParam k;
        DoubleParam T1;
        DoubleParam T2;

        public IntegrDiff(string name)
            : base(name)
        {
            outport = new OutPort("outport", this);
            base.RegisterOutport(outport);
            inport = new InPort("inport", this);
            base.RegisterInport(inport);
            step = new DoubleParam("step");
            base.RegisterParam(step);
            step.value = 0.005;
            k = new DoubleParam("k");
            base.RegisterParam(k);
            k.value = 1.0;
            T1 = new DoubleParam("T1");
            base.RegisterParam(T1);
            T1.value = 1.0;
            T2 = new DoubleParam("T2");
            base.RegisterParam(T2);
            T2.value = 1.0;
        }


        double out_old = 0;
        double in_old = 0;

        public override void Fire(double step)
        {
            double k1 = (double)T1.value / step;
            double k2 = (double)T2.value / step;
            List<IEntry> d = inport.Get();
            if (d.Count > 0)
            {
                out_old = ((double)k.value * ((k1 + 1) * d[0].data.value - k1 * in_old) + k2 * out_old) / (k2 + 1);
                in_old = d[0].data.value;
                //out_old = (k2 - k1) * out_old / (k2 + 1 - (double)k.value * (inport.Get()[0].value)*(k1+1));
                outport.Put(out_old);
            }
        }
        public override void ReInitialize()
        {
            base.ReInitialize();
             out_old = 0;
             in_old = 0;
        }


    }
}
